package lpca.view
{
	import flash.display.ColorCorrection;
	import flash.text.TextFieldType;
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.MovieClip;	
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IEventDispatcher;	
	import flash.text.TextField;	
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import kioschool.basic.view.View;
	import lpca.controllers.LpcaController
	import lpca.model.LpcaModel;
	import lpca.utils.Cell;
	import lpca.utils.Point;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import lpca.utils.Point;		
	import lpca.utils.ContainerViewCells;	
	import mx.controls.Label;
	/**
	 * ...
	 * @author 
	 */	
	public class ViewCell extends Sprite implements View
	{								
		private var point: Point; 		
		private var stateCell: Cell;
		private var model: LpcaModel;	
		private var defaultAlpha: Number;						
		private var showDisplayObject: DisplayObject;
		private var maskDisplayObject: DisplayObject;
		private var textField: TextField;
				
		public function show(): void
		{																					
			this.stateCell.visible = true;			
			showDisplayObject.visible = this.stateCell.visible;
			maskDisplayObject.visible = !this.stateCell.visible;
		}
			
		public function hide(): void
		{																				
			this.stateCell.visible = false;							
			showDisplayObject.visible = this.stateCell.visible;
			maskDisplayObject.visible = !this.stateCell.visible;			
		}
		
		private function addRadialFilter(): void
		{			
			var gradientGlow:GradientGlowFilter = new GradientGlowFilter(); 
			gradientGlow.distance = 0; 
			gradientGlow.angle = 15; 
			gradientGlow.colors = [0x000000, 0x0000FF]; 
			gradientGlow.alphas = [1, 0]; 
			gradientGlow.ratios = [0, 255]; 
			gradientGlow.blurX = 25; 
			gradientGlow.blurY = 25; 
			gradientGlow.strength = 2; 
			gradientGlow.quality = BitmapFilterQuality.HIGH; 
			var filter: Array = this.filters;			
			filter.unshift(gradientGlow);
			this.filters = filter;	
		}
		
		private function removeRadialFilter(): void
		{
			this.filters = [];
		}
			
		
		private function addTextField(): void
		{															
			if (this.getChildByName(this.textField.name) == null)
			this.addChild(textField);
		}
		
		private function removeTextField(): void
		{			
			if (this.getChildByName(this.textField.name) !== null)
				this.removeChild(this.textField);
		}
		
		private function showWaveValue(): void
		{			
			this.textField.textColor = 0x0000FF;
			this.textField.text = this.stateCell.valueWave.toString();
			this.addTextField();
			this.stateCell.visibleValueWave = true;
			
		}
		
		private function hideWaveValue(): void
		{
			if (this.getChildByName(this.textField.name) !== null)
			this.removeChild(textField);
			this.stateCell.visibleValueWave = false;
		}
		
		private function chooseWaveByStep(): void
		{
			this.textField.text = this.model.step.toString();
			this.textField.textColor = 0xFF0000;
			this.addTextField();
		}
		
		private function deChooseWaveByStep(): void
		{			
			this.removeTextField();
		}
		
		private function chooseTraceByStep(): void
		{
			this.addRadialFilter();
		}
		
		private function deChooseTraceByStep(): void
		{
			this.removeRadialFilter();
		}
		
		private function choose(): void
		{						
			this.stateCell.checked = true;				
			if (model.stag == LpcaModel.stageWave)
				this.chooseWaveByStep();
			else
				this.addRadialFilter();
		}
		
		private function deChoose(): void
		{
			this.stateCell.checked = false;
			if (model.stag == LpcaModel.stageWave)
				deChooseWaveByStep()
			else if (model.stag == LpcaModel.stageTrace)
				deChooseTraceByStep()
		}
				
		private function select(mouseEvent: MouseEvent): void
		{		
			this.alpha = 0.2;			
		}
		
		private function deSelect(mouseEvent: MouseEvent):void
		{	
			this.alpha = 1;			
		}

		
		public function getPoint(): Point
		{
			return new Point(this.point.x, this.point.y);
		}
		
			
		public function update(): void
		{																				
			var cell: Cell = model.getCell(this.point);									
			if (cell.visible && !stateCell.visible) 
				show()
			else if (!cell.visible && stateCell.visible)
				hide()						
			if (cell.checked && !stateCell.checked) 
				this.choose();
			else if (!cell.checked && stateCell.checked)
				this.deChoose();					
			if (cell.visibleValueWave && !stateCell.visibleValueWave)
				this.showWaveValue();
			else if (!cell.visibleValueWave && stateCell.visibleValueWave)
				this.hideWaveValue();
		}
		
		public function ViewCell(point: Point, model: LpcaModel,  handler: Function, showDisplayObject: DisplayObject,
		maskDisplayObject: DisplayObject)
		{												
			super();			
			this.model = model;						
			this.showDisplayObject = showDisplayObject;
			this.maskDisplayObject = maskDisplayObject;
			this.cacheAsBitmap = true;
			this.textField = new TextField();			
			this.textField.y = 5;
			this.textField.x = 10;			
			this.textField.type = TextFieldType.DYNAMIC;				
			//this.textField.focusEnabled = false;
			this.textField.selectable = false;									
			this.textField.setTextFormat(new TextFormat("Arial"));			
			this.point = point;			
			
			this.addChild(showDisplayObject);
			this.addChild(maskDisplayObject);			
			//Добавление слушателя из контроллера		
			if (handler !== null)
				this.addEventListener(MouseEvent.CLICK, handler);							
			this.addEventListener(MouseEvent.MOUSE_OVER, this.select);
			this.addEventListener(MouseEvent.MOUSE_OUT, this.deSelect);			
				
				
			try
			{
				this.stateCell = model.getCell(this.point);
				if (this.stateCell == null)
					throw new ReferenceError("Ошибка: невозможно определить тип ячейки.");
			}		
			catch (error: ReferenceError)
			{
				trace(error.message);
			}
			if (stateCell.visible) 
				show()
			else 
				hide()			
			if (stateCell.checked) 
				this.choose();
			else 
				this.deChoose();			
		}
		
	}

}